package com.guosen.web.mocker

import com.github.kevinsawicki.http.HttpRequest
import org.apache.commons.io.FileUtils

/**
 * Created by kerry on 2016/10/25.
 * add batch meta information from files
 */
if (!args.size()) {
    println 'no file given'
    return
}

String path = args[0]
def f = new File(path)

if (!f.exists()) {
    println 'not a file/directory - ' + path
    return
} else {
    println 'path - ' + path
}

String sys = args.size() == 2 ? args[1] : 'test'
final String url = 'http://localhost:8989/add'

def postOneFile = { File it ->
    println 'begin ' + it.name
    String body = it.getText('utf-8')
    println HttpRequest.post(url + '?sys=' + sys).contentType('text/plain').send(body).body()
}

if (f.isFile()) {
    postOneFile(f)
} else if (f.isDirectory()) {
    String[] suf = ['groovy']
    Iterator it = FileUtils.iterateFiles(f, suf, true)
    while (it.hasNext()) {
        postOneFile(it.next())
    }
}